package com.example.homeworkmanagement.controller;


import com.example.homeworkmanagement.dto.ClassesAndStudentsAndSubmissionsAndAssignmentsDTO;
import com.example.homeworkmanagement.pojo.Submissions;
import com.example.homeworkmanagement.service.SubmissionsService;
import com.example.homeworkmanagement.vo.ResultVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 拒绝加班
 * @since 2025-05-25
 */
@RestController
@RequestMapping("/submissions")
@Api(tags="学生作业管理",description="提供管理API接口")
@CrossOrigin
public class SubmissionsController {

    @Autowired
    private SubmissionsService submissionsService;

    /**
     * 学生重新提交作业（更新附件）
     */
    @PostMapping("/addAndUpdateSubmissionStd")
    @ApiOperation("学生重新提交作业（允许更新附件）")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "assignmentId", value = "作业ID", required = true, dataType = "integer"),
            @ApiImplicitParam(name = "studentId", value = "学生ID", required = true, dataType = "string"),
            @ApiImplicitParam(name = "file", value = "新作业附件（DOCX文件）", required = true, dataType = "file")
    })
    public ResultVO<Submissions> addAndUpdateSubmissionStd(Integer assignmentId, String studentId,MultipartFile file) {
        return submissionsService.saveOrUpdateSubmission(assignmentId, studentId,file);
    }
    /**
     * 删除作业提交记录（学生撤回）
     */
    @DeleteMapping("/deleteSubmission")
    @ApiOperation("删除作业提交记录")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "assignmentId", value = "作业ID", required = true, dataType = "integer"),
            @ApiImplicitParam(name = "studentId", value = "学生ID", required = true, dataType = "string")
    })
    public ResultVO<Submissions> deleteSubmission(Integer assignmentId,String studentId) {
        return submissionsService.deleteSubmission(assignmentId, studentId);
    }

    /**
     * 查询学生作业提交记录（学生/教师查看）
     */
    @GetMapping("/getSubmission")
    @ApiOperation("查询单个学生的作业提交记录")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "assignmentId", value = "作业ID", required = true, dataType = "Integer"),
            @ApiImplicitParam(name = "studentId", value = "学生ID", required = true, dataType = "String")
    })
    public ResultVO<ClassesAndStudentsAndSubmissionsAndAssignmentsDTO> getSubmission(Integer assignmentId, String studentId) {
        return submissionsService.getSubmission(assignmentId,studentId);
    }

    /**
     * 教师批改作业（更新成绩和评语）
     */
    @PostMapping("/updateSubmissionTea")
    @ApiOperation("教师批改作业")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "assignmentId", value = "作业ID", required = true, dataType = "Integer"),
            @ApiImplicitParam(name = "studentId", value = "学生ID", required = true, dataType = "String"),
            @ApiImplicitParam(name = "grade", value = "成绩", required = true, dataType = "String"),
            @ApiImplicitParam(name = "feedback", value = "教师评语", required = false, dataType = "String")
    })
    public ResultVO<Submissions> updateSubmissionTea(Integer assignmentId, String studentId, String grade, String feedback) {
        return submissionsService.updateSubmissionTea(assignmentId, studentId, grade, feedback);
    }

    /**
     * 按班级和作业查询作业提交列表（教师端班级作业统计）
     */
    @PostMapping("/getSubmissionList")
    @ApiOperation("按班级和作业查询作业提交列表（分页）")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "classIds", value = "班级ID串（逗号分隔）", required = true, dataType = "String"),
            @ApiImplicitParam(name = "assignmentId", value = "作业ID", required = true, dataType = "Integer"),
            @ApiImplicitParam(name = "page", value = "页码（从1开始）", required = true, dataType = "Integer", defaultValue = "1"),
            @ApiImplicitParam(name = "size", value = "每页大小", required = true, dataType = "Integer", defaultValue = "10")
    })
    public ResultVO<List<ClassesAndStudentsAndSubmissionsAndAssignmentsDTO>> getSubmissionList(String classIds, Integer assignmentId, Integer page, Integer size) {
        System.out.println("接收参数 - page: " + page + ", size: " + size);
        // 处理 page 为 null 的情况，设置默认值
        if (page == null || page <= 0) {
            page = 1;
        }
        // 处理 size 为 null 的情况，设置默认值
        if (size == null || size <= 0) {
            size = 10;
        }
        return submissionsService.getSubmissionList(classIds, assignmentId, page, size);
    }

    /**
     * 查看学生是否有提交
     */
    @PostMapping("/isSubmission")
    @ApiOperation("查看学生是否有提交")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "assignmentId", value = "作业ID", required = true, dataType = "integer"),
            @ApiImplicitParam(name = "studentId", value = "学生ID", required = true, dataType = "string")
    })
    public ResultVO<String> isSubmission(Integer assignmentId,String studentId) {
        return submissionsService.isSubmission(assignmentId,studentId);
    }

}
